package coop.bancocredicoop.proyectos.gd.query.customer;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.Transaction;

import coop.bancocredicoop.proyectos.gd.domain.Customer;
import coop.bancocredicoop.proyectos.gd.hibernate.HibernateUtil;

public class FindCustomerByIdPersona {

	public Customer run(Long idPersona) throws Exception {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		Customer result = null;

		try {
			tx = session.beginTransaction();

			result = run(idPersona, session);

			tx.commit();
		}
		catch (Exception e) {
			if (tx!=null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

		return result;

	}

	public Customer run(Long idPersona, Session session) throws Exception {

		Query query = session.createQuery("from Customer where idPersona = :idPersona");
		query.setLong("idPersona", idPersona);
		return (Customer) query.uniqueResult();

	}

}
